from DBA.dbmanager import DBInterface
from SalleDAC import Salle
class Batiment:
    """
    Classe d acces aux batiments references dans la base
    Table: PR5QTBA
    """   
    def __init__(self,id=""):
        """
        Constructeur
        @param id identificateur du batiment: soit on le rentre, soit il sera genere automatiquement.
        """
        self.id            = {"key":"ID","value":id}
        self.name        = {"key":"nom","value":""}
        self.description    = {"key":"description","value":""}
        self.xposition        = {"key":"x","value":""}
        self.yposition        = {"key":"y","value":""}
        self.zposition        = {"key":"z","value":""}
        self.height        = {"key":"realheight","value":""}
        self.width        = {"key":"realwidth","value":""}
        self.urlPlan        = {"key":"url_plan","value":""}
        self.ensSalles        = []
        self.__interfacebase    = DBInterface()
        if not id:
            self.id["value"]= str(self.__interfacebase.createNewID());
    
    def createByDico(self,dico):
        """
        Creation d'un batiment depuis un dictionnaire defini dans l'IHM
        @param dico dictionnaire genere par l'IHM en page2.py
        """
        self.name["value"]         = dico[self.name["key"]]
        self.description["value"]     = dico[self.description["key"]]
        self.xposition["value"]     = dico[self.xposition["key"]]
        self.yposition["value"]     = dico[self.yposition["key"]]
        self.zposition["value"]     = dico[self.zposition["key"]]
        self.height["value"]         = dico[self.height["key"]]
        self.width["value"]         = dico[self.width["key"]]
        self.urlPlan["value"]         = dico[self.urlPlan["key"]]

    def addSalle(self,salle):
        """
        Ajout d'une salle au batiment
        @param salle Objet de type Salle()
        """
        self.ensSalles.append(salle)
    
    def nbSalles(self):
        """
        Renvoit le nombre de salles pour ce batiment
        """
        return self.ensSalles.count()
    
    def selectBatimentFromDB(self):
          """
          Renvoit un tableau avec les infos du batiment en cours
          """
          return self.__interfacebase.selectTable(self.__interfacebase.batimenttablename,self.__interfacebase.batimentmapping)

    def selectSallesFromDB(self):
        """
        Retourne les salles associees au batiment courant
        """
        key ={}
        key["link_ba"] = self.id["value"]
        self.ensSalles = self.__interfacebase.selectTable(self.__interfacebase.salletablename,self.__interfacebase.sallemapping,key)
        return self.ensSalles

    def selectAccessPointFromDB(self):
        """
        Retourne les AP associes au batiment courant
        """
        self.selectSallesFromDB()
        ensAP = []
        for salle in self.ensSalles:
            s = Salle(salle["ID"])
            s.createByDico(salle)
            list = s.selectAccessPointFromDB()
            for AP in list:
                ensAP.append(AP)
        return ensAP

    def insertToDb(self):
        """
        Insertion du batiment en cours dans la BDD
        """
        dico = self.createBatimentDico()
        self.__interfacebase.insert(self.__interfacebase.batimenttablename,self.__interfacebase.batimentmapping,dico)
        for salle in self.ensSalles:
            salle.insertToDb(self.id["value"])
        
    def deleteFromDb(self):
        """
        Suppression de la BDD du batiment courant
        """
        key ={}
        key["ID"] = self.id["value"]
        self.selectSallesFromDB()
        #On initialise au cas ou il n'y aurai pas de salle
        elementsuppr=0
        for salle in self.ensSalles:
            s = Salle(salle["ID"])
            elementsuppr=elementsuppr + int(s.deleteFromDb())
        elementsuppr=elementsuppr + int(self.__interfacebase.deleteFromTable(self.__interfacebase.batimenttablename,self.__interfacebase.batimentmapping,key))
        return elementsuppr

    def updateIntoDb(self):
        """
        Mise a jour des informations du batiment courant dans la BDD
        """
        dico = self.createBatimentDico()
        self.__interfacebase.update(self.__interfacebase.batimenttablename,self.__interfacebase.batimentmapping,dico)
        for salle in self.ensSalles:
            salle.updateIntoDb(self.id["value"])
    
    def createBatimentDico(self):
        """
        Creation du dictionnaire equivalent au batiment courant
        """
        dico ={}
        dico[self.id["key"]]            = self.id["value"]
        dico[self.name["key"]]            = self.name["value"]
        dico[self.description["key"]]        = self.description["value"]
        dico[self.xposition["key"]]        = self.xposition["value"]
        dico[self.yposition["key"]]        = self.yposition["value"]
        dico[self.zposition["key"]]        = self.zposition["value"]
        dico[self.height["key"]]        = self.height["value"]
        dico[self.width["key"]]            = self.width["value"]
        dico[self.urlPlan["key"]]        = self.urlPlan["value"]
        return dico
